/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.source.linkedin;

import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.LinkedInApi;
import org.scribe.exceptions.OAuthException;
import org.scribe.model.Token;
import org.scribe.oauth.OAuthService;

import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.xml.sax.SAXException;

/**
 *
 * @author magzupao
 */
public class ObjetoLinkedIn implements Serializable {

    private static final long serialVersionUID = 1L;
    private static String API_KEY = "wkuog9fy54tu";   //wkuog9fy54tu  magz
    private static String API_SECRET = "Pzrt28c9DH2nHmCR";   //Pzrt28c9DH2nHmCR magz
    private OAuthService service = null;
    private Token requestToken = null;
    private String urlValidacion;
    private String respuestaXML;    
    //private ResourceBundle bundle;

    public String getUrlValidacion() {
        return urlValidacion;
    }

    public void setUrlValidacion(String urlValidacion) {
        this.urlValidacion = urlValidacion;
    }

    public String getRespuestaXML() {
        return respuestaXML;
    }

    public void setRespuestaXML(String respuestaXML) {
        this.respuestaXML = respuestaXML;
    }

//    public ResourceBundle getBundle() {
//        return bundle;
//    }
//
//    public void setBundle(ResourceBundle bundle) {
//        this.bundle = bundle;
//    }


	public ObjetoLinkedIn() {
    }

    public String validarContraLinkedIn() throws OAuthException {

        service = new ServiceBuilder().provider(LinkedInApi.class).apiKey(API_KEY).apiSecret(API_SECRET).build();
        requestToken = service.getRequestToken();
        urlValidacion = service.getAuthorizationUrl(requestToken);
        return urlValidacion;
    }

    public StringBuilder extraerDatos(String codigo) throws Exception {

        Verifier verifier = new Verifier(codigo);
        Token accessToken = service.getAccessToken(requestToken, verifier);

        String url = "http://api.linkedin.com/v1/people/~:(id,"
                + "headline,"
                + "first-name,"
                + "last-name,"
                + "maiden-name,"
                + "formatted-name,"
                + "phonetic-first-name,"
                + "phonetic-last-name,"
                + "formatted-phonetic-name,"
                + "specialties,"
                + "location:(name),"
                // da error  + "location:(country:(code)),"
                + "distance,"
                + "last-modified-timestamp,"
                + "current-share,"
                + "network,"
                + "connections,"
                + "num-connections,"
                + "industry,"
                + "summary,"
                + "proposal-comments,"
                + "associations,"
                + "patents,"
                + "volunteer,"
                + "honors,"
                + "publications,"
                + "interests,"
                + "languages,"
                + "skills,"
                + "certifications,"
                + "educations,"
                + "courses,"
                + "num-recommenders,"
                + "recommendations-received,"
                + "phone-numbers,"
                + "im-accounts,"
                + "twitter-accounts,"
                + "primary-twitter-account,"
                + "bound-account-types,"
                + "mfeed-rss-url,"
                + "following,"
                + "job-bookmarks,"
                + "group-memberships,"
                + "suggestions,"
                + "date-of-birth,"
                + "main-address,"
                + "member-url-resources,"
                //da error         + "member-url-resources:(url),"
                //da error         + "member-url-resources:(name),"
                + "picture-url,"
                + "site-standard-profile-request,"
                + "api-standard-profile-request:(url),"
                //   da error     + "api-standard-profile-request:(headers),"
                + "public-profile-url,"
                + "related-profile-views,"
                + "positions)";
        //+ "positions:(company:(name)))";


        OAuthRequest request = new OAuthRequest(Verb.GET, url);
        service.signRequest(accessToken, request);
        Response response = request.send();
        try {
        	StringBuilder sb = new StringBuilder();
        	sb.append(response.getBody());
        	System.out.println("XML de datos enviado de linkedin :");
        	System.out.println(sb.toString());
            return procesaXml(sb);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ObjetoLinkedIn.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(ObjetoLinkedIn.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ObjetoLinkedIn.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public StringBuilder procesaXml(StringBuilder cadenaDatos) throws ParserConfigurationException, SAXException, IOException {
        FormatJDOM fj = new FormatJDOM();
        //fj.setBundle(getBundle());
        StringBuilder datos = new StringBuilder();
        datos.append(fj.doProceso(cadenaDatos.toString()));
        return datos;
    }
}
